package cn.com.truelore;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.com.truelore.base.BaseTabActivity;
import cn.com.truelore.common.Constants;

/**
 * android应用程序启动的主程序
 * 
 * @author 牟瑞
 * 
 */
public class PriceActivity extends BaseTabActivity {

	private int _TabType = 0;

	int[] selected = new int[] { R.drawable.menu_corn_on,
			R.drawable.menu_bean_on, R.drawable.menu_wheat_on,
			R.drawable.menu_settings_on };
	int[] unSelected = new int[] { R.drawable.menu_corn_off,
			R.drawable.menu_bean_off, R.drawable.menu_wheat_off,
			R.drawable.menu_settings_off };

	public PriceActivity() {
		super(R.layout.price_main);
	}

	/**
	 * 创建手机的主界面的内容
	 */
	public void initPageData() {
/*		String strStpe4Completed = super.getSharedPreferences(
				Constants.SP_SETTING, Constants.SP_SETTING_STEP4);
		if (Constants.SP_SETTING_STEP4_COMPLETE.equals(strStpe4Completed) == true) {
			_TabType = this.getIntent().getIntExtra("tab", Constants.TYPE_CORN);
		} else {
			_TabType = Constants.TYPE_SETTINGS;
		}*/

		final TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
		Intent intent = new Intent(this, TabContentPriceActivity.class);
		Bundle bd = new Bundle();
		bd.putInt("type", Constants.TYPE_CORN);
		intent.putExtras(bd);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("", null)
				.setContent(intent));
		intent = new Intent(this, TabContentPriceActivity.class);
		bd = new Bundle();
		bd.putInt("type", Constants.TYPE_SOY);
		intent.putExtras(bd);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("", null)
				.setContent(intent));
		intent = new Intent(this, TabContentPriceActivity.class);
		bd = new Bundle();
		bd.putInt("type", Constants.TYPE_WHEAT);
		intent.putExtras(bd);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("", null)
				.setContent(intent));
		mTabHost.addTab(mTabHost
				.newTabSpec("tab_test4")
				.setIndicator("", null)
				.setContent(
						new Intent(this, TabContentSettingActivity.class)
								.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
		// setCurrentTab();
		TabWidget tabWidget = mTabHost.getTabWidget();
		int count = tabWidget.getChildCount();
		for (int i = 0; i < count; i++) {
			View view = tabWidget.getChildTabViewAt(i);
			final TextView tv = (TextView) view
					.findViewById(android.R.id.title);
			tv.setVisibility(8);
			view.setOnTouchListener(new View.OnTouchListener() {
				public boolean onTouch(View v, MotionEvent event) {
					return tabView_OnTouch(v, event);
				}
			});
		}

		mTabHost.setPadding(mTabHost.getPaddingLeft(),
				mTabHost.getPaddingTop(), mTabHost.getPaddingRight(),
				mTabHost.getPaddingBottom() - 5);

		updateTabBackground(mTabHost);

		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			public void onTabChanged(String tabId) {
				updateTabBackground(mTabHost);
			}
		});

	}

	private boolean tabView_OnTouch(View v, MotionEvent event) {
		boolean result = false;
		SharedPreferences sharedata = getSharedPreferences(
				Constants.SP_SETTING, 0);
		String strStpe4Completed = sharedata.getString(
				Constants.SP_SETTING_STEP4, "");
		/*if (Constants.SP_SETTING_STEP4_COMPLETE.equals(strStpe4Completed) == false) {
			// TODO
			result = true;
		}*/
		return result;
	}

	/**
	 * @param tabHost
	 * 
	 */
	private void updateTabBackground(TabHost tabHost) {

		for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
			View vvv = tabHost.getTabWidget().getChildAt(i);
			ImageView imageView = (ImageView) vvv
					.findViewById(android.R.id.icon);
			if (tabHost.getCurrentTab() == i) {
				imageView.setBackgroundDrawable(getResources().getDrawable(
						selected[i]));
				vvv.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.menu_bg_on));
			} else {
				imageView.setBackgroundDrawable(getResources().getDrawable(
						unSelected[i]));
				vvv.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.menu_bg_off));
			}
		}
	}

	private void setCurrentTab() {
		TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		if (Constants.TYPE_CORN == _TabType) {
			mTabHost.setCurrentTab(0);
		} else if (Constants.TYPE_SOY == _TabType) {
			mTabHost.setCurrentTab(1);
		} else if (Constants.TYPE_WHEAT == _TabType) {
			mTabHost.setCurrentTab(2);
		} else if (Constants.TYPE_SETTINGS == _TabType) {
			mTabHost.setCurrentTab(3);
		} else {
			mTabHost.setCurrentTab(0);
		}
	}
}
